/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package regulatoryInteractionsStats.yeast;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import units.Gene;
import utils.fishersExact.FishersExactTest;

/**
 *
 * @author jamie
 */
public class AlignmentStats {

    private double prior = 0.5;

    public AlignmentStats() {
        YeastGeneLibrary yeastGeneLibrary = new YeastGeneLibrary();
        AlignmentSet alignmentSet = new AlignmentSet(yeastGeneLibrary);
        yeastGeneLibrary.printStats();

        RegultoryInteractionSet regultoryInteractionSet = new RegultoryInteractionSet(yeastGeneLibrary);

//        SimilarityFileParser sfp_cc = getParser("CC");
        for (double i = 0; i <= 1; i += 0.02) {
            printDetails(i, regultoryInteractionSet, alignmentSet);
        }
    }

    private void printDetails(double cutoff, RegultoryInteractionSet regultoryInteractionSet, AlignmentSet alignmentSet) {
        ProportionStat propReg = getProportion_reg(regultoryInteractionSet, alignmentSet, cutoff);
        ProportionStat propAll = getProportion_all(regultoryInteractionSet, alignmentSet, cutoff);
        FishersExactTest fet = new FishersExactTest(propAll.getTotal(), propAll.getCount(), propReg.getTotal(), propReg.getCount());
        double pGP = ((propReg.getCount() + 0.0) / propReg.total) * prior;
        double x = ((propAll.getCount() - propReg.getCount() + 0.0) / propAll.getTotal()) * (1 - prior);
        double b = pGP / (pGP + x);
        System.out.println(cutoff + ", noReg: " + propReg.getCount() + ", totalReg: " + propReg.getTotal() + ", propReg: " + propReg.getProp() + ", noAll: " + propAll.getCount() + ", totalAll: " + propAll.getTotal() + ", propAll: " + propAll.getProp() + ", fc: " + fet.getFoldChange() + ", fet: " + fet.getResult() + ", bayes: " + b);
    }

    private ProportionStat getProportion_reg(RegultoryInteractionSet regultoryInteractionSet, AlignmentSet alignmentSet, double cutoff) {
        int total = 0;
        int count = 0;
        for (String symbolPair : regultoryInteractionSet.getRegulatoryInteractions()) {
            String[] symbols = symbolPair.split(":");
            YeastGene yg1 = regultoryInteractionSet.getYeastGeneLibrary().getGeneBySymbol(symbols[0]);
            YeastGene yg2 = regultoryInteractionSet.getYeastGeneLibrary().getGeneBySymbol(symbols[1]);

            String[] arr = {yg1.getSymbol(), yg2.getSymbol()};
            Arrays.sort(arr);
            String key = arr[0] + ":" + arr[1];
            if (alignmentSet.getScores().containsKey(key)) {
                double score = alignmentSet.getScores().get(key);
                if (score >= cutoff) {
                    count++;
                }
                
            }
            total++;

            
        }
//        System.out.println("reg: " + total + ", " + count);
        ProportionStat stat = new ProportionStat(count, total);
        return stat;
    }

    private ProportionStat getProportion_all(RegultoryInteractionSet regultoryInteractionSet, AlignmentSet alignmentSet, double cutoff) {
        int total = 0;
        int count = 0;
        ArrayList<String> geneSymbols = new ArrayList<String>();
        geneSymbols.addAll(regultoryInteractionSet.getGenes());
//        System.out.println(geneSymbols.size());
        for (int i = 0; i < geneSymbols.size(); i++) {
            String symbol1 = geneSymbols.get(i);
            YeastGene yg1 = regultoryInteractionSet.getYeastGeneLibrary().getGeneBySymbol(symbol1);
            for (int j = i + 1; j < geneSymbols.size(); j++) {
                String symbol2 = geneSymbols.get(j);
                YeastGene yg2 = regultoryInteractionSet.getYeastGeneLibrary().getGeneBySymbol(symbol2);
                String[] arr = {yg1.getSymbol(), yg2.getSymbol()};
                Arrays.sort(arr);
                String key = arr[0] + ":" + arr[1];

                if (alignmentSet.getScores().containsKey(key)) {
                    double score = alignmentSet.getScores().get(key);
                    if (score >= cutoff) {
                        count++;
                    }
                    
                }
                total++;
            }
        }
//        System.out.println("all: " + total + ", " + count);
        ProportionStat stat = new ProportionStat(count, total);
        return stat;
    }

    private class ProportionStat {

        int count;
        int total;

        public ProportionStat(int count, int total) {
            this.count = count;
            this.total = total;
        }

        public double getProp() {
            return (count + 0.0) / total;
        }

        public int getCount() {
            return count;
        }

        public int getTotal() {
            return total;
        }
    }
}
